//
//  AccountLoginController.m
//  easystudy
//
//  Created by 陈彦超 on 16/8/30.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "AccountLoginController.h"
#import "RegisterController.h"
#import "ForgetPswViewController.h"

@interface AccountLoginController ()

@end

@implementation AccountLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.title = @"登录";
    
    
    [_accountView drawCornerRadius:5];
    [_accountView drawBorderColor:[UIColor lightGrayColor] Width:1];
    
    [_pswView drawCornerRadius:5];
    [_pswView drawBorderColor:[UIColor lightGrayColor] Width:1];
    
    
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image_backBtn"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtn)];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerBtn)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    _accountTF.bounds = CGRectMake(0, 0, 0, 50);
    
}

- (void)backBtn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)registerBtn
{
    //去注册界面
    RegisterController *registerVC = [[RegisterController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (IBAction)forgetPsw:(UIButton *)sender
{
    //去忘记密码界面
    ForgetPswViewController *forgetPswVC = [[ForgetPswViewController alloc] init];
    [self.navigationController pushViewController:forgetPswVC animated:YES];
}

- (IBAction)weiBoBtnClick:(UIButton *)sender
{
    //去微博登录界面
    NSLog(@"微博登录");
}


- (IBAction)loginBtn:(UIButton *)sender
{
    [self hideKeyboard];
    if ([_accountTF.text length] == 0) {
        [self.view makeToast:@"请输入账号"];
        return;
    }
    if ([_pswTF.text length] == 0) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    NSDictionary *param = @{@"regphone":_accountTF.text,@"userpwd":_pswTF.text};
    
    [WebService loginWithParams:param WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"data"][@"code"] boolValue]) {
                [UserEntity sharedUser].UserID = [NSString stringWithFormat:@"%@",result[@"data"][@"custId"]];
                [UserEntity sharedUser].loginSuccess = YES;
                [UserEntity sharedUser].MobilePhone = _accountTF.text;
                [UserEntity sharedUser].Password = _pswTF.text;
                [[NSNotificationCenter defaultCenter] postNotificationName:NotificationLoginSuccess object:@1];
                [self dismissViewControllerAnimated:YES completion:nil];
            }else{
                [self.view makeToast:@"登录失败"];
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
        
    }];
    
}


//隐藏键盘
- (void)hideKeyboard
{
    [_accountTF resignFirstResponder];
    [_pswTF resignFirstResponder];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self hideKeyboard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
